package com.servingcloud.servera.modules.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.servingcloud.common.model.RestPage;
import com.servingcloud.common.model.RestResult;
import com.servingcloud.servera.modules.entity.UserInfo;
import com.servingcloud.servera.modules.model.UserInfoQUERY;
import com.servingcloud.servera.modules.service.UserInfoService;

import io.swagger.annotations.ApiOperation;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private UserInfoService userInfoService;
	 @Autowired HttpServletRequest request;
    private String getHostInfo() {
    	return request.getLocalName() +":"+request.getServerPort();
    }
    @ApiOperation(httpMethod="GET", value="获取用户分页", notes="", response=String.class)
    @RequestMapping("/findUserInfoList")
    public Mono<RestResult<RestPage<UserInfo>>> findUserInfoList(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize){
    	RestResult<RestPage<UserInfo>> ret = new RestResult<RestPage<UserInfo>>();
    	ret.setSuccess(true);
    	ret.setErrorMsg("我的地址："+getHostInfo());
    	try {
    		UserInfoQUERY param = new UserInfoQUERY();
    		ret.setReturnValue(userInfoService.findUserInfoPage(param));
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
        return Mono.just(ret);
    }
    
    //返回一个实体
    @GetMapping("{msg}")
    public Mono<String> sayHelloWorld(@PathVariable("msg") String msg) {
        System.out.println("come on " + msg);
        return Mono.just("servera receive : " +msg);
    }
    //返回一个列表
    @GetMapping("list")
    public Flux<Integer> list() {
        List<Integer> list = new ArrayList<>();
        list.add(8);
        list.add(22);
        list.add(75);
        list.add(93);
        Flux<Integer> userFlux = Flux.fromIterable(list);
        return userFlux;
    }
}